package a0.commonAPI.string;

import org.junit.Test;

import java.util.Arrays;

/**
 * String类型实际上是由字符数组存储的
 * private final char value[];
 * <p>
 * 所有类型的数据存储都会反映到基本数据类型的存储
 * <p>
 * String类是final Class 不能被继承
 */
public class StringDemo {

    /**
     * 两种赋值方式
     */
    @Test
    public void test1() {

        //通过new关键字创建对象赋值，常量池中没有该常量的时候
        // 会创建两个对象
        String s1 = new String("小王");
        //直接赋值
        String s2 = "小王";

        String s3 = "小王";

    }

    /**
     * 4种情况分析
     * 直接赋值字符串时，并把字符串连接时，考虑编译期和运行期
     * 如果【值】在编译期可以确定，就使用已有对象
     * 不确定，则会创建新对象
     * <p>
     * 总结：final关键字的作用
     */
    @Test
    public void test2() {

        //值确定
        String a = "a";
        //a是变量，所以a1不确定
        String a1 = a + "1";
        //值确定
        String a2 = "a1";
        System.out.println(a1 == a2);

        //值确定
        final String b = "b";
        //b是常量,值确定
        String b1 = b + "1";
        //值确定
        String b2 = "b1";
        System.out.println(b1 == b2);

        //值确定
        String c = getC();
        //c运行期才能确定值,值不确定
        String c1 = c + "1";
        //值确定
        String c2 = "c1";
        System.out.println(c1 == c2);


        //值确定
        final String d = getD();
        //d运行期才能确定值,值不确定
        String d1 = d + "1";
        //值确定
        String d2 = "d1";
        System.out.println(d1 == d2);

    }

    private static String getC() {
        return "c";
    }

    private static String getD() {
        return "d";
    }


    @Test
    public void test3() {

        String str = "1234567a7b7cdefg";

        String.format(str, "");

        //跟索引值获取字符
        char c = str.charAt(1);
        System.out.println(c);

        //获取字节数组
        byte[] bytes = str.getBytes();
        System.out.println(Arrays.toString(bytes));

        //是否以 ** 开始
        boolean startsWith = str.startsWith("123");
        System.out.println(startsWith);

        //是否以 ** 开始  加上偏移量
        boolean startsWithOffset = str.startsWith("234", 1);
        System.out.println(startsWithOffset);

        //是否以 ** 结束
        boolean endsWith = str.endsWith("fg");
        System.out.println(endsWith);

        //替换字符
        String newStr = str.replace('7', '-');
        System.out.println("newStr: " + newStr);

        //String str = "1234567a7b7cdefg";
        String replaceStr = str.replaceAll("[5-9]", "#");
        System.out.println("replaceStr:  " + replaceStr);

        //截取字符串
        String subStr1 = str.substring(3);
        System.out.println("subStr1:  " + subStr1);
        //截取字符串 包含索引3位置的字符，不包含索引6位置的字符
        String subStr2 = str.substring(3, 6);
        System.out.println("subStr2:  " + subStr2);

        //拆分字符串
        String str1 = "1 2 3 4 5-67a7b7-cd-efg";
        String[] split = str1.split("-");
        System.out.println("split:  " + Arrays.toString(split));

        String[] split2 = str1.split("-", 2);
        System.out.println("split2:  " + Arrays.toString(split2));

        /**
         * 字符串查找
         */
        String str2 = "1234567a7b7cdefg";
        boolean contains = str2.contains("5-67");
        System.out.println("contains: " + contains);

        //获取字符索引
        int index = str2.indexOf("7");
        System.out.println("index: " + index);
        int fromIndex = str2.indexOf("7", 8);
        System.out.println("fromIndex: " + fromIndex);

        int indexChar = str2.indexOf('2');
        int fromIndexChar = str2.indexOf('2', 6);
        System.out.println("indexChar: " + indexChar);

        int i = str2.indexOf("7");
        System.out.println(i);
        int i1 = str2.lastIndexOf("7");
        System.out.println(i1);

        //参数：Unicode code point
        int i3 = str2.lastIndexOf(52);
        System.out.println(i3);
    }


    /**
     * 字符串其他操作
     */
    @Test
    public void test4() {
        String str = " ";
        System.out.println(str.isEmpty());
        System.out.println(str.length());

        String s2 = "abcDEF";
        System.out.println(s2.toLowerCase());
        System.out.println(s2.toUpperCase());

        String s3 = " abc DEF ";
        System.out.println(s3.trim());

        System.out.println(s2.concat(s3));
        //把其他类型的数转换为 String类型
        System.out.println(String.valueOf(5));

    }


}
